package com.epichust.bootumpp.service.impl;

import com.epichust.bootumpp.model.bo.FiltersBO;
import com.epichust.bootumpp.model.bo.Umpp5PlanOrderBO;
import com.epichust.bootumpp.model.bo.Umpp5PlanOrderPublishBO;
import com.epichust.bootumpp.model.bo.Umpp5TaskOrderBO;
import com.epichust.bootumpp.model.dao.Umpp5PlanOrderDao;
import com.epichust.bootumpp.model.dao.Umpp5TaskOrderDao;
import com.epichust.bootumpp.model.dao.Umpp5TaskOrderScheduleDao;
import com.epichust.bootumpp.model.entity.*;
import com.epichust.bootumpp.service.ISerialCodeService;
import com.epichust.bootumpp.service.IUmpp5PlanOrderService;
import com.epichust.bootumpp.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：longyang
 * @date ：Created in 2021/1/12 11:47
 * @description：
 * @modified By：
 * @version: $
 */
@Service
@Transactional
public class Umpp5PlanOrderServiceImpl implements IUmpp5PlanOrderService {

    @Autowired
    private Umpp5PlanOrderDao umpp5PlanOrderDao;

    @Autowired
    private Umpp5TaskOrderDao umpp5TaskOrderDao;

    @Autowired
    private Umpp5TaskOrderScheduleDao umpp5TaskOrderScheduleDao;

    @Autowired
    private ISerialCodeService iSerialCodeService;

    /**
     * 分页查询订单信息
     * @param map
     * @return
     */
    @Override
    public List<Umpp5PlanOrderBO> getAllUmpp5PlanOrder(Map<String, Object> map) {
        if(map.get("orderCode") != null){
            String [] orderCodeArr = map.get("orderCode").toString().split(",");
            map.put("orderCode",orderCodeArr);
        }
        //处理分页信息数据
        if(map.get("rows") != null && map.get("page") != null){
            int rows = Integer.parseInt(map.get("rows").toString());
            int page = Integer.parseInt(map.get("page").toString());
            int firstLimit = (page-1)*rows;
            int lastLimit = page*rows;
            map.put("firstLimit",firstLimit);
            map.put("lastLimit",lastLimit);
        }
        //处理前端查询信息
        //filters=[{"logic":"","left":"","right":"","field":"plannedBeginDate","data":"2021-01-14","compare":"ge"},{"logic":"","left":"","right":"","field":"plannedBeginDate","data":"2021-02-10","compare":"le"}]
        if(map.get("filters") != null){
            String filtersStr = map.get("filters").toString();
            List<FiltersBO> filters = GsonUtils.parseString2List(filtersStr,FiltersBO.class);
            for(FiltersBO bo :filters){
                if("plannedBeginDate".equals(bo.getField()) && "ge".equals(bo.getCompare())){//计划开始时间大于
                    map.put("plannedBeginDateGe",bo.getData()+" 00:00:00");
                }else if("plannedBeginDate".equals(bo.getField()) && "le".equals(bo.getCompare())){//计划开始时间小于
                    map.put("plannedBeginDateLe",bo.getData()+" 23:59:59");
                }else if("plannedEndDate".equals(bo.getField()) && "ge".equals(bo.getCompare())){
                    map.put("plannedEndDateGe",bo.getData() + " 00:00:00");
                }else if("plannedEndDate".equals(bo.getField()) && "le".equals(bo.getCompare())){
                    map.put("plannedEndDateLe",bo.getData() + " 23:59:59");
                }else{
                    map.put(bo.getField(),bo.getData());
                }
            }
        }
        String records = umpp5PlanOrderDao.getAllUmpp5PlanOrderRecords(map);
        List<Umpp5PlanOrderBO> list = umpp5PlanOrderDao.getAllUmpp5PlanOrder(map);
        if(!list.isEmpty()){
            list.get(0).setRecords(records);
        }
        return list;
    }

    /**
     * 订单发布
     * @param list
     */
    @Override
    public void publishOrder(List<Umpp5PlanOrderPublishBO> list) throws Exception{
        for(Umpp5PlanOrderPublishBO bo : list){
            Umpp5TaskOrder taskOrder = new Umpp5TaskOrder();
            Map<String,String> cmap = new HashMap<String, String>();
            cmap.put("head","TO");
            cmap.put("middle","date");
            cmap.put("waterFlag","0");
            cmap.put("connectFlag","-");
            cmap.put("waterNumber","5");
            //生成派工单号
            String code = iSerialCodeService.generateSerialCode(cmap);
            taskOrder.setCode(code);
            taskOrder.setOpCode(bo.getOpCode() == null ? "" : bo.getOpCode());
            taskOrder.setMrlCode(bo.getMrlCode() == null ? "" : bo.getMrlCode());
            taskOrder.setWbsNumber(bo.getWbsNumber());
            taskOrder.setOrderCode(bo.getCode());
            taskOrder.setCreateId(bo.getCreateId() == null ? "SYSTEM" : bo.getCreateId());
            taskOrder.setCreateDate(new Date());
            taskOrder.setQty(bo.getRemainQty() == null ? BigDecimal.ZERO : bo.getRemainQty());
            taskOrder.setModifyId(bo.getModifyId() == null ? "SYSTEM" : bo.getModifyId());
            taskOrder.setModifyDate(new Date());
            taskOrder.setSiteCode(bo.getSiteCode() == null ? "DEFAULT" : bo.getSiteCode());
            taskOrder.setWorkCenterCode(bo.getWorkCenterCode() == null ? "DEFAULT" : bo.getWorkCenterCode());
            taskOrder.setDrawing("DEFAULT");
            //订单发布保存信息到派工单表
            umpp5TaskOrderDao.insertUmpp5TaskOrder(taskOrder);

            Umpp5TaskOrderState taskOrderState = new Umpp5TaskOrderState();
            taskOrderState.setState("1");
            taskOrderState.setTaskOrderCode(taskOrder.getCode());
            taskOrderState.setSiteCode(taskOrder.getSiteCode() == null ? "SYSTEM" : taskOrder.getSiteCode());
            taskOrderState.setWorkCenterCode(taskOrder.getWorkCenterCode() == null ? "SYSTEM" : taskOrderState.getWorkCenterCode());
            //订单发布保存信息到派工单记录表
            umpp5TaskOrderDao.insertUmpp5TaskOrderState(taskOrderState);

            Umpp5TaskOrderSchedule umpp5TaskOrderSchedule = new Umpp5TaskOrderSchedule();
            umpp5TaskOrderSchedule.setCode(taskOrder.getCode());
            umpp5TaskOrderSchedule.setOpCode(taskOrder.getOpCode());
            umpp5TaskOrderSchedule.setMrlCode(taskOrder.getMrlCode());
            umpp5TaskOrderSchedule.setWbsNumber(taskOrder.getWbsNumber());
            umpp5TaskOrderSchedule.setOrderCode(taskOrder.getOrderCode());
            umpp5TaskOrderSchedule.setCreateId(taskOrder.getCreateId());
            umpp5TaskOrderSchedule.setCreateDate(new Date());
            umpp5TaskOrderSchedule.setQty(taskOrder.getQty());
            umpp5TaskOrderSchedule.setModifyId(taskOrder.getModifyId());
            umpp5TaskOrderSchedule.setModifyDate(new Date());
            umpp5TaskOrderSchedule.setSiteCode(taskOrder.getSiteCode());
            umpp5TaskOrderSchedule.setWorkCenterCode(taskOrder.getWorkCenterCode());
            umpp5TaskOrderSchedule.setDrawing(taskOrder.getDrawing());
            //订单发布时，保存信息至派工单排程表
            umpp5TaskOrderScheduleDao.insertUmpp5TaskOrderSchedule(umpp5TaskOrderSchedule);

            Umpp5PlanOrderState umpp5PlanOrderState = new Umpp5PlanOrderState();
            umpp5PlanOrderState.setOrderCode(taskOrder.getOrderCode());
            umpp5PlanOrderState.setSiteCode(taskOrder.getSiteCode());
            umpp5PlanOrderState.setWorkCenterCode(taskOrder.getWorkCenterCode());
            Map<String,String> tmap = new HashMap<>();
            tmap.put("proRouteLineCode",bo.getCode());
            List<Umpp5TaskOrderBO> tlist = umpp5TaskOrderDao.getAllUmpp5TaskOrderByProRouteLineCode(tmap);
            //累加相同订单下，所有派工单的计划数量
            BigDecimal tqty = BigDecimal.ZERO;
            for(Umpp5TaskOrderBO tbo : tlist){
                tqty = tqty.add(tbo.getQty());
            }
            if(bo.getQty().compareTo(tqty) <= 0){
                umpp5PlanOrderState.setState("3");//当订单的计划数量小于其对应所有派工单计划数量的时候，设置为发布
            }else{
                umpp5PlanOrderState.setState("2");//设置为部分发布
            }
            //新增订单状态表信息
            umpp5PlanOrderDao.insertUmpp5PlanOrderState(umpp5PlanOrderState);
        }
    }
}
